
package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;

import org.json.JSONArray;

import android.content.Context;
import android.text.TextUtils;

import com.xiaoma.myaudience.biz.model.HotVideoInfo;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.Pair;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 异步加载热门信息.
 * 
 */
public class HotInfoTask extends
        BaseDataAsyncTask<Void, Void, Pair<String, ArrayList<HotVideoInfo>>> {
    private final static String TAG = "HotInfoTask";

    public final static String HOT_FILM = "hot_film";

    public final static String HOT_TV = "hot_tv";

    private String mType;

    /**
     * {@inheritDoc}
     * 
     * @param context
     * @param taskType
     * @param callback
     * @param type 热门类型如:{@link #HOT_FILM} {@link #HOT_FILM}
     */
    public HotInfoTask(Context context, int taskType, DataAsyncCallback callback, String type) {
        super(context, taskType, callback);
        mType = type;
    }

    @Override
    protected Pair<String, ArrayList<HotVideoInfo>> doInBackground(Void... params) {
        ArrayList<HotVideoInfo> hots = null;
        String url = null;
        if (HOT_FILM.equals(mType)) {
            url = Constant.URL_HOT_FILM;
        } else if (HOT_TV.equals(mType)) {
            url = Constant.URL_HOT_TV;
        }

        if (!TextUtils.isEmpty(url)) {
            try {
                // 获取热播信息
                JSONArray jsonArray = HttpUtils.getHttpJSONArrayResult(getHttpClient(), url, null,
                        HttpUtils.GET, mContext);
                if (jsonArray != null) {
//                    String imgSize = PicUtils.composeSize((int) mContext.getResources().getDimension(
//                            R.dimen.list_item_img_width), (int) mContext.getResources().getDimension(
//                            R.dimen.list_item_img_height));
                    hots = new ArrayList<HotVideoInfo>();
                    HotVideoInfo info;
                    final int size = jsonArray.length();
                    for (int i = 0; i < size; i++) {
                        info = new HotVideoInfo();
                        info.readFromJSONObject(jsonArray.getJSONObject(i));
                        hots.add(info);
//                        ImageLoadingTask.getBitmap(mContext, getHttpClient(),
//                                info.mImgUrlPath, imgSize, false);
                    }
                }
            } catch (Exception e) {
                Logger.e(TAG, "doInBackground error!!!", e);
            } finally {
                closeHttpClient();
            }
        }
        return Pair.create(mType, hots);
    }
}
